package xmq

import (
	"context"
	"gitee.com/xlizy/common-go/base/common_config"
	"gitee.com/xlizy/common-go/base/common_rpc"
	"gitee.com/xlizy/common-go/base/response"
	"gitee.com/xlizy/common-go/utils/common_utils"
	"gitee.com/xlizy/common-go/utils/threadlocal"
	"gitee.com/xlizy/common-go/utils/zlog"
	"gitee.com/xlizy/rpc-interface/rpc_pbs"
)

func Send(topic, msg string) *response.Response {
	zlog.Debug("发送MQ消息,topic:{},msg:{}", topic, msg)
	extraParam := make(map[string]string)
	extraParam["ProducerClientIp"] = common_utils.GetLocalPriorityIp(common_config.PriorityNetwork.Networks)
	extraParam["ProducerApp"] = common_config.GetAppName()
	extraParam["ProducerTraceId"] = threadlocal.GetTraceId()
	rsp := common_rpc.R(common_rpc.XMQServiceClientImpl().SendXMQ(context.TODO(), &rpc_pbs.SendXMQReq{
		Topic:      topic,
		Msg:        msg,
		ExtraParam: extraParam,
	}))

	if rsp.Success {
		sendXMQResp := &rpc_pbs.SendXMQResp{}
		rsp.RpcData(sendXMQResp)
		zlog.Info("发送XMQ成功msgKey:{}", sendXMQResp.MsgKey)
		return response.Success("发送成功", sendXMQResp.MsgKey)
	} else {
		zlog.Info("发送MQ消息失败,topic:{},msg:{}", topic, msg)
		return rsp
	}
}
